package servlet;

import com.alibaba.fastjson.JSONObject;
import org.json.HTTP;
import service.UserService;
import service.UserServiceImpl;
import utils.JsonUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.zip.CheckedOutputStream;

/**
 * @author lishunlin110
 */
@WebServlet(name = "ShopListAddServlet", value = "/shoppingList/add")
public class ShopListAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json; charset=utf-8");
        response.setHeader("Access-Control-Allow-Origin","*");
        PrintWriter out = response.getWriter();
        String userId = request.getParameter("u_id");
        String content = request.getParameter("content");
        System.out.println(userId);
        System.out.println(content);
        UserService service = new UserServiceImpl();
        try {
            int result = service.addShoppingList(userId,content);
            if(result!=0){
                out.println("T");
            }else {
                out.println("F");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            out.println("E");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        String userId = request.getParameter("u_id");
        String content = request.getParameter("content");
        System.out.println(userId);
        System.out.println(content);
        UserService service = new UserServiceImpl();
        try {
            int result = service.addShoppingList(userId,content);
            System.out.println(result);
            if(result!=0){
                out.println("T");
            }else {
                out.println("F");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            out.println("E");
        }
    }
}
